#include "poststelecomapi.h"

#include <httpclient.h>

PostsTelecomAPI::PostsTelecomAPI() {}

QString PostsTelecomAPI::name()
{
    return "人民邮电出版社 - 搜索";
}

QString PostsTelecomAPI::prefix()
{
    return "https://www.ptpress.com.cn/shopping/buy?bookId=";
}

QList<QListWidgetItem *> PostsTelecomAPI::searching_item(const QString &text)
{
    Request req("https://www.ptpress.com.cn/bookinfo/wsBookSearch");
    req.addQuery("rows", "1000");
    req.addQuery("page", "1");
    req.addQuery("keyword", text);

    auto rows = HttpClient::instance().get(req).toMap().value("rows").toList();
/**
 * 数据来源随机参考
    {
        "picPath": "https://cdn.ptpress.cn/uploadimg/Material/978-7-115-57372-8/72jpg/57372_s300.jpg",
        "hbPhotoExist": "N",
        "originBookCode": "157372",
        "author": "彭凌西 唐春明 陈统",
        "price": 69.9,
        "stockInDate": "20211225",
        "isbn": "978-7-115-57372-8",
        "shopType": 1,
        "bookName": "从零开始 Qt可视化程序设计基础教程",
        "executiveEditor": "张天怡",
        "bookId": "bff87a22-bf07-4a73-b348-f65d4dc5c5be"
    }
*/

    QList<QListWidgetItem *> list;
    foreach (auto var, rows) {
        auto executiveEditor = var.toMap().value("executiveEditor").toString();
        auto bookName = var.toMap().value("bookName").toString();
        auto bookId = var.toMap().value("bookId").toString();
        auto item = newItem();
        item->setText(QString("[%1]《%2》").arg(executiveEditor).arg(bookName));
        item->setData(990, bookId);
        list << item;
    }
    return list;
}

QString PostsTelecomAPI::content_item(const QListWidgetItem *item)
{
    return "https://www.ptpress.com.cn/shopping/buy?bookId=" + item->data(990).toString();
}
